
#ifndef __DC_CHAN_H_
#define __DC_CHAN_H_




#define HREG_ID         0x02
#define HREG_BYPASS     0x0C
#define HREG_HCR        0x0D
#define HREG_HSR        0x0E
#define HREG_EHSR       0x0F
#define HREG_WBBR       0x11
#define HREG_PSR        0x12
#define HREG_PC         0x13
#define HREG_IR         0x14
#define HREG_CSR        0x15
#define HREG_DDER       0x16
#define HREG_ADDR       0x18
#define HREG_DATA       0x19
#define HREG_BSEL       0x1E


/* 底层访问 had 寄存器. */
int had_null_clock( void );
int had_ir8_write( uint8_t vir );
int had_dr32_write( uint32_t vdr );
int had_dr32_read( uint32_t *pvdr );
int had_dr32_write_by_index( uint8_t vir, uint32_t vdr );
int had_dr32_read_by_index( uint8_t vir, uint32_t *pvdr );

/* 组合命令 */
int e902_enter_debug( void );
int e902_resume_exec( uint32_t epc );
int e902_write_memory( uint32_t addr, int tsiz, uint8_t * ptr );

int e902_read_csr( uint idx, uint32_t * pcsr );
int e902_write_csr( uint idx, uint32_t csrv );

int e902_read_gpr( uint idx, uint cnt, uint32_t * pgpr );
int e902_write_gpr( uint idx, uint cnt, uint32_t * pgpr );


/* 通道消息 */
int had_umsg_send( umsg_t * pmsg );



typedef struct _tag_async_session had_async_session_t;

typedef int (*async_resp_cbkf)( had_async_session_t * psess, int tnum, uint32_t * prsp );

struct _tag_async_session {

    /* single link list */
    struct _tag_async_session * next;
    bool autofree;
    uint32_t stats;

    /* request data */
    int req_num;
    uint32_t req_ary[4];

    /* call back function for resp */
    async_resp_cbkf cbkf;
    void * parg;
    int step;

};


/**/
int had_async_alloc_session( async_resp_cbkf cbkf, void * parg, int autofree, had_async_session_t ** ppsess );
int had_async_post_requst( had_async_session_t * psess, int tnum, uint32_t * preq );
int had_async_refresh_requst( had_async_session_t * psess, int tnum, uint32_t * preq );
int had_async_refresh_callbk( had_async_session_t * psess, async_resp_cbkf cbkf, void * parg );
int had_async_proc_resp( int tnum, uint32_t * prsp );

/**/
void had_umsg_null_clock( umsg_t * pmsg );
void had_umsg_dr32_write( umsg_t * pmsg );
void had_umsg_dr32_read( umsg_t * pmsg );

/**/
void had_umsg_debug_enter( umsg_t * pmsg );
void had_umsg_debug_leave( umsg_t * pmsg );
void had_umsg_ddc_write( umsg_t * pmsg );

void had_umsg_channel_start( umsg_t * pmsg );
void had_umsg_channel_stop( umsg_t * pmsg );


/* e902 */
void had_umsg_e902_csrr( umsg_t * pmsg );
void had_umsg_e902_csrw( umsg_t * pmsg );




int had_channel_forward_to_cpu( umsg_t * pmsg );


/**/
int had_mod_init(  uint tck, uint tms, uint32_t freq );

#endif

